{include file="common/head"/}
<div class="admin-main layui-anim layui-anim-upbit">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>传感器类型{:lang('list')}</legend>
    </fieldset>
    <div class="demoTable">
        <div class="layui-inline">
            <input class="layui-input" name="key" id="key" placeholder="请输入传感器类型">
        </div>
        <button class="layui-btn" id="search" data-type="reload">搜索</button>
        <a href="javascript:location.replace(location.href);" class="layui-btn">刷新</a>
        <a  class="layui-btn"   style="margin-right: 0px;float: right;" id="AddSensorType" data-type="reload" onclick="AddSensorType()"  >添加传感器类型</a>
        <div style="clear: both;"></div>
    </div>
    <table class="layui-table" id="list" lay-filter="list"></table>
</div>
<script type="text/html" id="operation">
    <a class="layui-btn layui-btn-normal layui-btn-xs" onclick="EditSensorType('{{d.id}}')" >编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script type="text/html" id="img">
    <img src="{{d.img}}" height="50px;" width="50px"
         onmouseover="layer.tips('<img   src={{d.img}}>',this,{tips: [1, '#fff'],area:'200px,200px'});"  onmouseout="layer.closeAll();">
</script>
{include file="common/foot"/}
<script src="/static/common/js/jquery.2.1.1.min.js"></script>
<script>
    //添加传感器类型
    function  AddSensorType() {
        layer.open({
            type: 2,
            title: '添加传感器类型',
            shade: 0.01,
            shadeClose: true,
            scrollbar: false,
            area: ['800px', '600px'],
            anim: 5,
            content: "{:url('AddSensorType')}"
        });
    }


    /**
     * 编辑
     */
    function  EditSensorType(id) {
        layer.open({
            type: 2,
            title: '编辑',
            shade: 0.01,
            shadeClose: true,
            scrollbar: false,
            area: ['800px', '600px'],
            anim: 5,
            content: "{:url('EditSensorType')}?id=" + id
        });
    }
</script>
<script>
    layui.use(['table','form'], function() {
        // 列表
        var table = layui.table,form = layui.form, $ = layui.jquery;
        var tableIn = table.render({
            id: 'user',
            elem: '#list',
            url: '{:url("SensorTypeList")}',
            method: 'post',
            page: true,
            cols: [[
                {field:'id', title: '传感器类型编号', width:150,fixed: true,align:'center'}
                ,{field:'name', title: '传感器类型名称',align:'center'}
                ,{field:'range', title: '正常范围',align:'center'}
                ,{field:'img', title: '传感器图片',toolbar:'#img'}
                , {field: 'is_del', align:'center', title: '操作', toolbar: ' #operation '}
            ]],
            limit: 10 //每页默认显示的数量
        });
        //状态开关
        form.on('switch(is_open)', function(obj){
            loading =layer.load(1, {shade: [0.1,'#fff']});
            var id = this.value;
            var is_open = obj.elem.checked===true?1:2;
            $.post('{:url("SensorTypeState")}',{'id':id ,'is_open':is_open},function (res) {
                layer.close(loading);
                console.log(res);
                if (res.code===1) {
                    layer.msg(res.msg,{time:1000,icon:1});
                    tableIn.reload();
                }else{
                    layer.msg(res.msg,{time:1000,icon:2});
                    return false;
                }
            })
        });
        table.on('tool(list)',function (obj) {
            var data = obj.data;
            //操作   删除
            if(obj.event==='del'){
                layer.confirm('确定删除吗？',function (index) {
                    $.post("{:url('SensorTypeDel')}",{id:data.id},function (res) {
                        console.log(res);
                        if(res.code===1){
                            layer.msg(res.msg,{time:1000,icon:1});
                            tableIn.reload();
                        }else{
                            layer.msg(res.msg,{time:1000,icon:2});
                        }
                    });
                    layer.close(index);
                })
            }

        });
        //搜索
        $('#search').on('click', function() {
            var key = $('#key').val();
            if($.trim(key)==='') {
                layer.msg('{:lang("pleaseEnter")}关键字！',{icon:0});
                return;
            }
            tableIn.reload({
                where: {key: key}
            });
        });

    });

</script>
</body>
</html>
